package com.itheima.controller;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/findByClassId")
    public Result findByClassId(HttpSession session) {
        Integer classId = (Integer) session.getAttribute("classId");
        List<User> users = userService.selectByClassId(classId);
        if (ObjectUtils.isEmpty(users)) {
            return new Result<>(1, "操作失败", null);
        } else {
            return new Result<>(0, "操作成功", users);
        }
    }

    @PostMapping("/addIntegral")
    public Result addIntegral(@RequestBody User user) {
        boolean flag = userService.updateIntegral(user);
        if (flag = true) {
            return new Result(0, "操作成功", "添加积分成功");
        } else {
            return new Result<>(1, "操作失败", null);
        }
    }

    /**
     *重置积分
     */
    @GetMapping("/resetGScoreByGroupId/{classId}")
    public Result resetGroupIntegral(Integer groupId) {
        boolean flag = userService.updateResetIntegral(groupId);
        if (flag == true) {
            return new Result(0, "操作成功", "重置成功");
        } else {
            return new Result(1, "操作失败", null);
        }
    }
}
